package us.mmapp.entities;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class FieldEntity {
	private long id;
	private String field;
	private String info;
	private int order_id;
	private int oid;
	public FieldEntity()
	{}
	
	public FieldEntity(JSONObject obj)
	{
		try
		{
			setId(obj.getLong("id"));
		} catch (JSONException e)
		{
			Log.e("FIELD ENTITY: ", e.getMessage());
		}
		try
		{
			setField(obj.getString("field"));
		} catch (JSONException e)
		{
			Log.e("FIELD ENTITY: ", e.getMessage());
		}
		try
		{
			setInfo(obj.getString("info"));
		} catch (JSONException e)
		{
			Log.e("FIELD ENTITY: ", e.getMessage());
		}
		try
		{
			setOrder_id(obj.getInt("order_id"));
		} catch (JSONException e)
		{
			Log.e("FIELD ENTITY: ", e.getMessage());
		}
		try
		{
			setOID(obj.getInt("oid"));
		} catch (JSONException e)
		{
			Log.e("FIELD ENTITY: ", e.getMessage());
		}
		
	}
	public FieldEntity(long id1, String field1, String info1, int order_id1, int oid1)
	{
		id=id1;
		field=field1;
		info=info1;
		order_id=order_id1;
		oid=oid1;
	}
	public JSONObject toJson()
	{
		JSONObject j=new JSONObject();
		try
		{
			j.put("id", id);
			j.put("field", field);
			j.put("info", info);
			j.put("order_id", order_id);
			j.put("oid", oid);
		}
		catch(JSONException e)
		{
			
		}
		return j;
	}
	public void insertDB(SQLiteDatabase db)
	{
		ContentValues cv=new ContentValues();
		cv.put("FIELD", field);
		cv.put("INFO", info);
		cv.put("ORDER_ID", order_id);
		cv.put("OID", oid);
		
		db.insertWithOnConflict("EXTRA_FIELDS", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
		//db.close();
	}
	public void setOID(int objectID)
	{
		oid=objectID;
	}
	public void setId(long id)
	{
		this.id = id;
	}

	public long getId()
	{
		return id;
	}

	public void setField(String field)
	{
		this.field = field;
	}

	public String getField()
	{
		return field;
	}

	public void setInfo(String info)
	{
		this.info = info;
	}

	public String getInfo()
	{
		return info;
	}

	public void setOrder_id(int order_id)
	{
		this.order_id = order_id;
	}

	public int getOrder_id()
	{
		return order_id;
	}
}
